package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: PREETHI
 * Date: 15/5/13
 * Time: 5:03 PM
 * To change this template use File | Settings | File Templates.
 */
public class framebooks extends JFrame{

    JList categories;
    JLabel dis;
    library lib;
    JPanel p;
    JSplitPane splitPane;
     JTable bookTable;
    JScrollPane bookScrollPane;
    String colHeaders[]={"BOOK ID","BOOK NAME","AUTHOR NAME","SELECT"};
     Object[][] data;
    static int categoryIndex;
    public framebooks()
    {

    }
    public framebooks(library lib)
    {
       this.lib=lib;
        lib.addBooks();
    }

    public JSplitPane listBooks() {


        categories =new JList((new Vector(lib.getCategoryList())));
        categories.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        categories.setSelectedIndex(0);
        categories.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList Categories, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component renderer = super.getListCellRendererComponent(Categories, value, index, isSelected, cellHasFocus);
                if (renderer instanceof JLabel && value instanceof category) {

                           ((JLabel) renderer).setText(((category) value).getCategoryName());
                }
                return renderer;
            }
        });
        categories.addListSelectionListener( new ListSelectionChangeHandler());
        p=new JPanel();
        bookTable=new JTable();
        JScrollPane listScrollPane = new JScrollPane(categories);
        listScrollPane.setMinimumSize(new Dimension(150, 70));
         bookScrollPane=new JScrollPane();
        bookScrollPane.setViewportView(bookTable);
        p.add(bookScrollPane);
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane,p);
        return splitPane;
    }

    public int reserveBooks(book b) {
      if(lib.getUsername() !=null)
      {
        if(b.getQuantity() >0)
        {
            JOptionPane.showMessageDialog(p,"Thank you! Enjoy the book!","Reserved",JOptionPane.INFORMATION_MESSAGE);
            b.setQuantity(b.getQuantity() - 1);
            return 1;
        }
        else
        {
            JOptionPane.showMessageDialog(p,"Sorry!The book is not available yet!","Unavailable",JOptionPane.INFORMATION_MESSAGE);
            return 0;

        }
      }
        else
          JOptionPane.showMessageDialog(p,"You must login to reserve book","Failure",JOptionPane.INFORMATION_MESSAGE);
        return 0;
    }

    class ListSelectionChangeHandler implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(!e.getValueIsAdjusting())
            {
             JList list = (JList)e.getSource();
             categoryIndex=list.getSelectedIndex() ;
             displayBooksOfSelectedCategory( lib.getCategoryList().get(categoryIndex)) ;


            bookScrollPane.setViewportView(bookTable);
            bookScrollPane.setPreferredSize(new Dimension(700,600));
            p.removeAll();
            p.add(bookScrollPane) ;
            splitPane.revalidate();
            splitPane.repaint();
            }
        }

        //display books of the selected category
        public void displayBooksOfSelectedCategory(category c)
        {
            java.util.List bl= c.getBookList();
            int size= c.getBookList().size();
            data=new Object[size][4];
            for(int i=0;i<size;i++)
            {
                book b= (book) bl.get(i);
                data[i][0]=  new Integer(b.getBookID());
                data[i][1]=  new String(b.getBookName());
                data[i][2]=  new String(b.getAuthorName());
                data[i][3]=  new JButton("RESERVE");

            }
            DefaultTableModel dm=new DefaultTableModel(data,colHeaders);
            bookTable=new JTable(dm){
                public boolean isCellEditable (int iRows, int iCols) {
                    if (iCols==3)
                    return true;
                    else return false;
                }
            public void tableChanged(TableModelEvent e) {
                super.tableChanged(e);
                repaint();
            }
        };
           bookTable.getColumn("SELECT").setCellRenderer(new ButtonRenderer());
           bookTable.getColumn("SELECT").setCellEditor(new ButtonEditor(new JCheckBox()));

        }
    }
    class ButtonRenderer implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

            if (value == null)
            {
                return null;
            }
            return (Component) value;


        }
    }
    class ButtonEditor extends DefaultCellEditor implements ActionListener {
        private JButton button;
        public int bookIndex;
        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                                                     boolean isSelected, int row, int column) {
            if (value == null)
                return null;
            button = (JButton) value;

            bookIndex=row;
            button.addActionListener(this);
            return (Component) value;
        }

        public Object getCellEditorValue() {
            button.removeActionListener(this);
            return button;
        }

        public void actionPerformed(ActionEvent e) {
            category c=(category) lib.getCategoryList().get(categoryIndex);
            book b=(book) c.getBookList().get(bookIndex);
            reserveBooks(b);
                                                     }
    }
}